#pragma once
#include "interfaces.h"
#include "d3d9util.h"
#include <vector>

class SceneNode2DProperties
{
	friend class SceneNode2D;

protected:
	ActorId m_actorId;
	std::string m_name;
	Mat4x4 m_toWorld, m_fromWorld;
	D3DCOLOR m_color;
	Vec2 m_size;
	Vec2 m_position;
	double m_orientation;
	
public:
	ActorId const &ActorId() const { return m_actorId; }
	Mat4x4 const &ToWorld() const { return m_toWorld; }
	Mat4x4 const &FromWorld() const { return m_fromWorld; }
	D3DCOLOR const &Color() const { return m_color; }
	Vec2 const &Size() const { return m_size; }
	Vec2 const &Position() const { return m_position; }
	double const &Orientation() const { return m_orientation; }
	void Transform(Mat4x4 *toWorld, Mat4x4 *fromWorld) const;
	const char * Name() const { return m_name.c_str(); }
};

typedef std::vector<std::shared_ptr<ISceneNode2D>> SceneNode2DVector;

class SceneNode2D : public ISceneNode2D
{
public:
	SceneNode2D(std::string name, const Vec2 *pos, const Vec2 *size, double orientation = 0, ActorId aid = 0)
	{
		m_pParent = NULL;
		m_props.m_actorId = aid;
		m_props.m_name = name;
		m_props.m_color = 0xffffffff;
		m_props.m_size = *size;
		m_props.m_position = *pos;
		m_props.m_orientation = orientation;
		m_scale = Vec2(1,1);
		VSetTransform();
	}
	virtual ~SceneNode2D();

	virtual const SceneNode2DProperties* const VGet() const { return &m_props; }
	virtual void VSetTransform();
	virtual void VSetSize(const Vec2& size) { m_props.m_size = size; }
	virtual void VSetPosition(const Vec2& pos) { m_props.m_position = pos; }
	virtual void VSetOrientation(const double& ori) { m_props.m_orientation = ori; }

	virtual void VUpdate(Scene2D* pScene, double deltaSecs);
	virtual HRESULT VRestore(Scene2D* pScene);
	virtual HRESULT VPreRender(Scene2D *pScene);
	virtual bool VIsVisible(Scene2D * pScene);
	virtual HRESULT VRender(Scene2D *pScene);
	virtual HRESULT VRenderChildren(Scene2D *pScene);
	virtual HRESULT VPostRender(Scene2D *pScene);
	virtual bool VAddChild(std::shared_ptr<ISceneNode2D> kid);
	virtual bool VRemoveChild(ActorId id);
	virtual HRESULT VPick(Scene2D *pScene, const Vec2& Position);

protected:
	SceneNode2D *m_pParent;
	SceneNode2DVector m_children;
	SceneNode2DProperties m_props;
	Vec2 m_scale;
};

